#include "randomutils.h"
/*
 * makes a game state with random num players
 * and a random assortment of kingdom cards
 * But initializes no hands or other state
 */
struct gameState* random_game_state()
{
	struct gameState *G = (struct gameState*)malloc(sizeof(struct gameState));
	
	/* according to boardgamegeek.com max players should 
	 * be no more than 4 should play unless intrigue is 
	 * added.  Since some of the cards in our options
	 * we really should allow for up to * as specified by
	 * boardgamegeek, however our max players is defined
	 * at 4. So we won't be testing the case of > 4
	 */
	G->numPlayers = (rand() % 3) + 2;
	
	// -1 init the supply count
	int i;
	for(i = 0; i < treasure_map+1; i++)
	{
		G->supplyCount[i] = -1;
	}

	// initialize victory cards
	for(i = estate; i <= province; i++)
	{
		G->supplyCount[i] = 8;
		if(G->numPlayers > 2)
		{
			G->supplyCount[i] = G->supplyCount[i] + 4;
		}
	}
	// initialize curse cards
	G->supplyCount[curse] = 10 * (G->numPlayers - 1);
	
	// initialize kingdom cards
	int * kingdom = (int*)malloc(sizeof(int) * 10);
	for(i = 0; i < 10; i++)
	{
		kingdom[i] = (rand() % (treasure_map - adventurer)) + adventurer;
	}
	for(i = 0; i < 10; i++)
	{
		G->supplyCount[kingdom[i]] = 10;
	}

	// initialize treasure supplies
	for(i = copper; i <= gold; i++)
	{
		G->supplyCount[i] = 10;
	}

	return G;
}

/*
 * simply adds random cards to the deck from
 * the available supply cards left
 * makes sure to not put the game into a game over state
 */
void random_deck(int player, struct gameState *G)
{
	int num_cards = rand() % 60;
	int i;
	int card;
	for(i = 0; i < num_cards; i++)
	{
		// randomly select a card to add to the deck
		do
		{
			card = rand() % treasure_map;
		} while(G->supplyCount[card] == 0);
		
		G->supplyCount[card] = G->supplyCount[card] - 1;
		if(isGameOver(G))
		{
			G->supplyCount[card] = G->supplyCount[card] + 1;
			i--;
		}
		else if(G->deckCount[player] > MAX_DECK)
		{
			G->supplyCount[card] = G->supplyCount[card] + 1;
			break;
		}
		else
		{
			G->deck[player][G->deckCount[player]] = card;
			G->deckCount[player] = G->deckCount[player] + 1;
		}
	}
}

/*
 * Adds random cards to the discard from available supply cards left
 */
void random_discard(int player, struct gameState *G)
{
	int num_cards = rand() % 60;
	int i;
	int card;
	for(i = 0; i < num_cards; i++)
	{
		// randomly select a card to add to the deck
		do
		{
			card = rand() % treasure_map;
		} while(G->supplyCount[card] == 0);
		
		G->supplyCount[card] = G->supplyCount[card] - 1;
		if(isGameOver(G))
		{
			G->supplyCount[card] = G->supplyCount[card] + 1;
			i--;
		}
		else if(G->discardCount[player] > MAX_DECK)
		{
			G->supplyCount[card] = G->supplyCount[card] + 1;
			break;
		}
		else
		{
			G->discard[player][G->discardCount[player]] = card;
			G->discardCount[player] = G->discardCount[player] + 1;
		}
	}
}

/*
 * Adds random cards to hand from available supply cards left
 */
void random_hand(int player, struct gameState* G)
{
	int num_cards = rand() % 10;
	int i;
	int card;
	for(i = 0; i < num_cards; i++)
	{
		// randomly select a card to add to the deck
		do
		{
			card = rand() % treasure_map;
		} while(G->supplyCount[card] == 0);
		
		G->supplyCount[card] = G->supplyCount[card] - 1;
		if(isGameOver(G))
		{
			G->supplyCount[card] = G->supplyCount[card] + 1;
			i--;
		}
		else if(G->handCount[player] > MAX_HAND)
		{
			G->supplyCount[card] = G->supplyCount[card] + 1;
			break;
		}
		else
		{
			G->hand[player][G->handCount[player]] = card;
			G->handCount[player] = G->handCount[player] + 1;
		}
	}
}

/*
 * setup game state for certain player
 */
void setup_for_player(int player, struct gameState* G)
{
	G->whoseTurn = player;
	G->numActions = (rand() % 3) + 1;
	G->numBuys = (rand() % 3) + 1;
	G->phase = 0;
}
